import {
  app,
  BrowserWindow,
  ipcMain,
  Menu,
} from "electron";
const fs = require('fs');
const path = require("path");

// 忽略Electron的警告
process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true";
app.commandLine.appendSwitch("disable-web-security");
app.commandLine.appendSwitch("disable-features", "OutOfBlinkCors"); // 允许跨域
app.commandLine.appendSwitch("--ignore-certificate-errors", "true"); // 忽略证书相关错误

let win: null | BrowserWindow = null;

const createWindow = async () => {
  // Menu.setApplicationMenu(null);
  win = new BrowserWindow({
    title: "文西",
    width: 1000,
    height: 800,
    icon: path.join(__dirname, '../public/logo.ico'),
    webPreferences: {
      webSecurity: false,
      nodeIntegration: true,
      contextIsolation: true,
      webviewTag: true,
      preload: path.join(__dirname, "preload.js"),
    },
  });
  if (process.env.VITE_DEV_SERVER_URL) {
    win.loadURL(process.env.VITE_DEV_SERVER_URL);
    win.webContents.openDevTools();
  } else {
    win.loadFile(path.join(__dirname, "../dist/index.html"));
  }
};

app.whenReady().then(createWindow);
app.on("window-all-closed", () => {
  app.exit();
});

  // 读文件夹
ipcMain.handle('read-folder', async (event, folderPath) => {
  try {
    // 读取文件夹内容
    const files = fs.readdirSync(folderPath).map(file => ({
      name: file,
      isDirectory: fs.lstatSync(path.join(folderPath, file)).isDirectory(),
    }));
    return files; // 返回文件列表
  } catch (error) {
    return { error: error.message }; // 返回错误信息
  }
});

// 读具体文件内容
ipcMain.handle('read-file', async (event, filePath) => {
  try {
    const content = fs.readFileSync(filePath, 'utf-8'); // 读取文件内容
    return { success: true, content };
  } catch (error) {
    return { success: false, error: error.message }; // 返回错误信息
  }
});
  
ipcMain.on('consolelog', () => {
  console.log("打印 this is 111111")
})
ipcMain.on('consolelog_2', (event, data) => {
  console.log("打印 this is ",data)
})
ipcMain.on('consolelog_3', (event, data) => {
  console.log("打印 this is ",data)
})

  
  
  